package com.ruoyi.project.product.specification.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.product.specification.domain.Specification;
import com.ruoyi.project.product.specification.mapper.SpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationServiceImpl implements ISpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    /**
     * 规格查询
     * @param specification
     * @return
     */
    @Override
    public List<Specification> selectSpecificationList(Specification specification) {
        return specificationMapper.selectSpecificationList(specification);
    }

    /**
     * 规格新增
     * @param specification
     * @return
     */
    @Override
    public int insertSpecification(Specification specification) {
        return specificationMapper.insertSpecification(specification);
    }

    /**
     * 根据id查询规格
     * @param specificationId
     * @return
     */
    @Override
    public Specification selectSpecificationById(Long specificationId) {
        return specificationMapper.selectSpecificationById(specificationId);
    }

    /**
     * 规格修改
     * @param specification
     * @return
     */
    @Override
    public int updateSpecification(Specification specification) {
        return specificationMapper.updateSpecification(specification);
    }

    /**
     * 规格批量删除操作
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public int deleteSpecificationByIds(String ids) throws Exception {
        return specificationMapper.deleteSpecificationByIds(Convert.toLongArray(ids));
    }

    /**
     * 校验规格名称是否存在
     * @param specification
     * @return
     */
    @Override
    public String checkSpecificationNameUnique(Specification specification) {
        //判断id是否为空，是返回-1，不是获取id
        Long specificationId = StringUtils.isNull(specification.getSpecificationId()) ? -1L : specification.getSpecificationId();
        //根据规格名称查询表里的规格名称是否存在
        Specification info = specificationMapper.checkSpecificationNameUnique(specification.getSpecificationName());
        //如果info不为空，并且，表里的specificationId跟传进来的specificationId不相等的话，返回一个1，不然返回一个0
        if (StringUtils.isNotNull(info) && info.getSpecificationId().longValue() != specificationId.longValue())
        {
            return UserConstants.SERIES_NAME_NOT_UNIQUE;
        }
        return UserConstants.SERIES_NAME_UNIQUE;
    }
}
